<?php
include_once '../Utilities/connect.php';
$conn=  getConnect();
mysql_set_charset('utf8',$conn);
function searchCategory($str)
{
     $conn=  getConnect();
     $arr=array();
    $sql="select categoryID,CategoryName from categories CategoryName like '%".$str."%' ";
    $result=  mysql_query($sql, $conn);  
    while ($row = mysql_fetch_array($result)) {
        $arr[]=$row;
    }
    return $arr;
    mysql_close($conn);
}
function searchProduct($str)
{
     $conn=  getConnect();
     $arr=array();
    $sql="select ProductID, ProductName,Description,Price,Image from product where ProductName Like '%".$str."%' or Price Like '%".$str."%'";
    $result=  mysql_query($sql, $conn);  
    if(!$result)
        die('Error Query');
    while ($row = mysql_fetch_array($result)) {
        $arr[]=$row;
    }
    return $arr;
    mysql_close($conn);
}
// lay top san pham duoc ban chay nhat
function getTopProduct($str)
{
    $conn=  getConnect();
    $arr=array();
    $sql="SELECT distinct(ord.ProductID), ProductName, Description, Price,Image
        FROM orderdetail ord, Product pr
        WHERE ord.ProductID = pr.ProductID and ProductName like '%".$str."%'
        ORDER BY ProductID DESC
        LIMIT 0 , 10";
    $result=  mysql_query($sql, $conn);     
    if(!$result)
        die('Error Query');
    while ($row = mysql_fetch_array($result)) {
        $arr[]=$row;
    }
    return $arr;
    mysql_close($conn); 
}
function searchPrice($str)
{
     $conn=  getConnect();
     $arr=array();
    $sql="select ProductID, ProductName,Description,Price,Image from product where Price Like '%".$str."%'";
    $result=  mysql_query($sql, $conn);  
    if(!$result)
        die('Error Query');
    while ($row = mysql_fetch_array($result)) {
        $arr[]=$row;
    }
    return $arr;
    mysql_close($conn);
}
function searchDetail($ProductID)
{
     $conn=  getConnect();
     $arr=array();
    $sql="select * from product where ProductID='$ProductID'";
    $result=  mysql_query($sql, $conn);  
    if(!$result)
        die('Error Query');
    while ($row = mysql_fetch_array($result)) {
        $arr[]=$row;
    }
    return $arr;
    mysql_close($conn);
}
function ProductDiscount($str)
{
     $conn=  getConnect();
     $arr=array();
        $sql="select * from product where ProductName like '%".$str."%' and Discount <>0";
        $result=  mysql_query($sql, $conn);  
        if(!$result)
            die('Error Query'.  mysql_error());
        while ($row = mysql_fetch_array($result)) {
            $arr[]=$row;
        }
        return $arr;
    mysql_close($conn);
}
?>
